CMAKE_MINIMUM_REQUIRED(VERSION 2.8.8)

SET(ASPECT_BINARY "${ASPECT_BINARY}" CACHE STRING "" FORCE)
SET(ASPECT_DIR "${ASPECT_DIR}" CACHE STRING "" FORCE)

DEAL_II_INITIALIZE_CACHED_VARIABLES()

MESSAGE(STATUS "Setting up tests with CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE}")

PROJECT(testsuite CXX)

ENABLE_TESTING()
SET_IF_EMPTY(TEST_TIME_LIMIT 240)

ADD_CUSTOM_TARGET(tests)

FIND_PROGRAM(NUMDIFF_EXECUTABLE
  NAMES numdiff
  HINTS ${NUMDIFF_DIR}
  PATH_SUFFIXES bin
  )

IF("${TEST_DIFF}" STREQUAL "")
  #

  IF(NOT NUMDIFF_EXECUTABLE MATCHES "-NOTFOUND")
    SET(TEST_DIFF ${NUMDIFF_EXECUTABLE})
  ELSEIF(NOT DIFF_EXECUTABLE MATCHES "-NOTFOUND")
    SET(TEST_DIFF ${DIFF_EXECUTABLE})
  ELSE()
    MESSAGE(FATAL_ERROR
      "Could not find diff or numdiff. One of those are required for running the testsuite.\n"
      "Please specify TEST_DIFF by hand."
      )
  ENDIF()
ENDIF()


FILE(GLOB _tests *.output)

SET(_n_tests "0")
LIST(SORT _tests)

FOREACH(_test ${_tests})

  SET(_test_full ${_test})
  GET_FILENAME_COMPONENT(_testname ${_test} NAME_WE)

 IF ("${_testname}" STREQUAL "")
    MESSAGE("Ignoring invalid .prm '${_test_full}'...")
    SET(_use_test OFF)
  ENDIF()
  IF (${_test_full} MATCHES "\\.x\\.prm$")
    # Skip files generated by in source builds:
    SET(_use_test OFF)
  ENDIF()

  SET(_test_prm "${_testname}.prm")
  STRING(REGEX MATCH "mpirun=([0-9]*)" NP ${_test})
  IF("${NP}" STREQUAL "")
    SET(NP 1)
  ELSE()
    STRING(REGEX REPLACE "^mpirun=([0-9]*)$" "\\1" NP ${NP})
  ENDIF()

  SET(_test_prm "${_testname}.prm")
  SET(_testname "${_testname}-np${NP}")
  SET(_test_ref_output "${_test}")
  STRING(REGEX REPLACE ".output$" ".statistics" _test_ref_statistics ${_test_ref_output})

  MESSAGE(STATUS "** processing test ${_testname}  NP=${NP}:")

   ADD_CUSTOM_TARGET(tests.${_testname}
        DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/output-${_testname}/screen-output)

  MATH(EXPR _n_tests "${_n_tests} + 1")

  FILE(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/output-${_testname})

  ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_testname}.x.prm
      COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/${_test_prm}
                 ${CMAKE_CURRENT_BINARY_DIR}/${_testname}.x.prm
      COMMAND echo ''
                 >> ${CMAKE_CURRENT_BINARY_DIR}/${_testname}.x.prm
      COMMAND echo 'subsection Global parameters'
                 >> ${CMAKE_CURRENT_BINARY_DIR}/${_testname}.x.prm
      COMMAND echo 'set Output directory = output-${_testname}'
                 >> ${CMAKE_CURRENT_BINARY_DIR}/${_testname}.x.prm
      COMMAND echo 'end'
                 >> ${CMAKE_CURRENT_BINARY_DIR}/${_testname}.x.prm
 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_test_prm}
      )

	SET(_replacement_script "${CMAKE_CURRENT_SOURCE_DIR}/default.pl")

ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/output-${_testname}/screen-output
        COMMAND mpirun -n ${NP} ${CMAKE_BINARY_DIR}/cracks ${CMAKE_CURRENT_BINARY_DIR}/${_testname}.x.prm >${CMAKE_CURRENT_BINARY_DIR}/output-${_testname}/screen-output.tmp
        COMMAND ${_replacement_script} screen-output <${CMAKE_CURRENT_BINARY_DIR}/output-${_testname}/screen-output.tmp >${CMAKE_CURRENT_BINARY_DIR}/output-${_testname}/screen-output
        DEPENDS ${ASPECT_BINARY} ${CMAKE_CURRENT_BINARY_DIR}/${_testname}.x.prm ${_testdepends})


# TODO
	ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/output-${_testname}/output.diff
        COMMAND
	echo "Check ${CMAKE_CURRENT_BINARY_DIR}/output-${_testname}/screen-output ${_test_ref_output}"
	COMMAND
	${CMAKE_CURRENT_SOURCE_DIR}/run_test.sh ${TEST_DIFF}
		${CMAKE_CURRENT_BINARY_DIR}/output-${_testname}/screen-output
		${_test_ref_output}
		${CMAKE_CURRENT_BINARY_DIR}/output-${_testname}
		screendiff

	COMMAND
	echo "Check ${CMAKE_CURRENT_BINARY_DIR}/output-${_testname}/statistics ${_test_ref_statistics}"
	COMMAND
	${CMAKE_CURRENT_SOURCE_DIR}/run_test.sh ${TEST_DIFF}
		${CMAKE_CURRENT_BINARY_DIR}/output-${_testname}/statistics
		${_test_ref_statistics}
		${CMAKE_CURRENT_BINARY_DIR}/output-${_testname}
		statdiff

	COMMAND touch ${CMAKE_CURRENT_BINARY_DIR}/output-${_testname}/output.diff
        DEPENDS ${_test_ref_output}
                ${CMAKE_CURRENT_BINARY_DIR}/output-${_testname}/screen-output)

	ADD_CUSTOM_TARGET(${_testname}-diff
        DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/output-${_testname}/output.diff)
        ADD_DEPENDENCIES(tests.${_testname} ${_testname}-diff)

 ADD_DEPENDENCIES(tests tests.${_testname})
    ADD_TEST(NAME ${_testname}
      COMMAND
         ${CMAKE_COMMAND}
            -DBINARY_DIR=${CMAKE_BINARY_DIR}
            -DTESTNAME=tests.${_testname}
            -DERROR="Test ${_testname} failed"
            -P ${CMAKE_CURRENT_SOURCE_DIR}/run_test.cmake
          WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
      )
    SET_TESTS_PROPERTIES(${_testname} PROPERTIES
          TIMEOUT ${TEST_TIME_LIMIT}
      )

ENDFOREACH()

MESSAGE(STATUS "Added ${_n_tests} tests.")
